/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.ui.viewer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JPanel;

import org.fba.prionace.file.JFile;

public class IconsViewer extends Viewer implements ComponentListener {

	protected JPanel iconsPane;

	private Dimension iconSize;

	public IconsViewer() {
		super();
		init();
	}

	public IconsViewer(File currentPath) throws FileNotFoundException {
		super(currentPath);
		init();
	}

	public IconsViewer(String currentPath) throws FileNotFoundException {
		super(currentPath);
		init();
	}

	private void init() {
		iconsPane = new JPanel(new FlowLayout(FlowLayout.LEADING));
		iconsPane.setBackground(Color.decode("0xF0F0F0"));
		refreshFilesList();
		setViewportView(iconsPane);
		addComponentListener(this);
	}

	@Override
	public void refreshFilesList() {
		super.refreshFilesList();
		refreshFiles();
	}

	@Override
	public void showRoots() {
		super.showRoots();
		refreshFiles();
	}

	private void refreshFiles() {
		if (iconsPane != null) {
			iconsPane.removeAll();

			for (JFile file : files) {
				file.setAutoSizeable(true);
				iconsPane.add(file);
			}

			iconsPane.revalidate();
			iconsPane.repaint();
			resizeViewer();
		}
	}

	/**
	 * Helper method for <code>componentResized</code>.
	 * <p>
	 * Adjusts the viewport's size.
	 */
	public void resizeViewer() {
		if (getViewport().getView() != null) {
			int width;
			int height;
			int columns = 0;
			int rows = 0;

			JPanel pane = (JPanel) getViewport().getView();

			if (iconSize == null) {
				int iwidth = 0;
				int iheight = 0;

				if (pane.getComponentCount() > 0) {
					iwidth = pane.getComponent(0).getSize().width + 5;
					iheight = pane.getComponent(0).getSize().height + 5;
				}

				iconSize = new Dimension(iwidth, iheight);
			}

			columns = getParent().getSize().width / iconSize.width;
			rows = pane.getComponentCount() / columns;
			width = columns * iconSize.width;
			height = rows * iconSize.height + iconSize.height;
			pane.setPreferredSize(new Dimension(width, height));
			getVerticalScrollBar().setValue(0);
		}
	}

	/**
	 * The class IconViewer has to implement the interface ComponentListener to
	 * fix an issue in the FlowLayout layout.
	 * <p>
	 * FlowLayout does not move components to the next line when there is no
	 * more horizontal space on its parent container. Customizing the method
	 * <code>componentResized</code> should make the trick.
	 * 
	 * @param e
	 *            The event which triggered this method.
	 * 
	 */
	public void componentResized(ComponentEvent e) {
		resizeViewer();
	}

	public void componentMoved(ComponentEvent e) {
	}

	public void componentShown(ComponentEvent e) {
	}

	public void componentHidden(ComponentEvent e) {
	}
}
